package org.easyway.tiles.generic;

import java.awt.Dimension;
import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;

public class NewWorldDialog extends JDialog {
	private static final long serialVersionUID = -8063476491556747104L;
	private JLabel jLabel = null;
	private JPanel jPanel = null;
	private JLabel jLabel1 = null;
	private JTextField filenametxt = null;
	private JButton changefilename_cmd = null;
	
	public String filename="C:\\world.wld";
	public int tileWidth=64;
	public int tileHeight=64;
	public int worldWidth=20;
	public int worldHeight=20;
	
	public boolean pressedOk = false;
	
	private JLabel jLabel2 = null;
	private JLabel jLabel21 = null;
	private JTextField worldWidthtxt = null;
	private JTextField worldHeighttxt = null;
	private JLabel jLabel3 = null;
	private JLabel jLabel31 = null;
	private JLabel jLabel22 = null;
	private JLabel jLabel211 = null;
	private JTextField tileWidthtxt = null;
	private JTextField tileHeighttxt = null;
	private JLabel jLabel4 = null;
	private JButton jButtonOK = null;
	private JButton jButtonCancel = null;

	/**
	 * This method initializes 
	 * 
	 */
	public NewWorldDialog(JFrame owner) {
		super(owner,true);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        jLabel = new JLabel();
        jLabel.setText("Enter the configuration for the new world to be created");
        jLabel.setBounds(new Rectangle(7, 5, 309, 16));
        this.setSize(new Dimension(528, 335));
        this.setContentPane(getJPanel());
        this.setTitle("Create a new world");
        filenametxt.setText(filename);
        tileHeighttxt.setText(String.valueOf(tileHeight));
        tileWidthtxt.setText(String.valueOf(tileWidth));
        worldHeighttxt.setText(String.valueOf(worldHeight));
        worldWidthtxt.setText(String.valueOf(worldWidth));
	}

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getJPanel() {
		if (jPanel == null) {
			jLabel4 = new JLabel();
			jLabel4.setBounds(new Rectangle(184, 165, 308, 23));
			jLabel4.setText("Power of 2 preferable, most common : 16,32,64,128");
			jLabel211 = new JLabel();
			jLabel211.setBounds(new Rectangle(3, 195, 80, 21));
			jLabel211.setText("Tile height :");
			jLabel22 = new JLabel();
			jLabel22.setBounds(new Rectangle(3, 165, 76, 21));
			jLabel22.setText("Tile width :");
			jLabel31 = new JLabel();
			jLabel31.setBounds(new Rectangle(183, 137, 24, 16));
			jLabel31.setText("tiles");
			jLabel3 = new JLabel();
			jLabel3.setBounds(new Rectangle(181, 102, 84, 23));
			jLabel3.setText("tiles");
			jLabel21 = new JLabel();
			jLabel21.setBounds(new Rectangle(3, 135, 91, 21));
			jLabel21.setText("World height :");
			jLabel2 = new JLabel();
			jLabel2.setBounds(new Rectangle(3, 105, 88, 21));
			jLabel2.setText("World width :");
			jLabel1 = new JLabel();
			jLabel1.setBounds(new Rectangle(3, 75, 63, 21));
			jLabel1.setText("Filename :");
			jPanel = new JPanel();
			jPanel.setLayout(null);
			jPanel.add(jLabel, null);
			jPanel.add(jLabel1, null);
			jPanel.add(getFilenametxt(), null);
			jPanel.add(getChangefilename_cmd(), null);
			jPanel.add(jLabel2, null);
			jPanel.add(jLabel21, null);
			jPanel.add(getWorldWidthtxt(), null);
			jPanel.add(getWorldHeighttxt(), null);
			jPanel.add(jLabel3, null);
			jPanel.add(jLabel31, null);
			jPanel.add(jLabel22, null);
			jPanel.add(jLabel211, null);
			jPanel.add(getTileWidthtxt(), null);
			jPanel.add(getTileHeighttxt(), null);
			jPanel.add(jLabel4, null);
			jPanel.add(getJButtonOK(), null);
			jPanel.add(getJButtonCancel(), null);
		}
		return jPanel;
	}

	/**
	 * This method initializes filenametxt	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getFilenametxt() {
		if (filenametxt == null) {
			filenametxt = new JTextField();
			filenametxt.setBounds(new Rectangle(100, 75, 316, 21));
		}
		return filenametxt;
	}

	/**
	 * This method initializes changefilename_cmd	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getChangefilename_cmd() {
		if (changefilename_cmd == null) {
			changefilename_cmd = new JButton();
			changefilename_cmd.setBounds(new Rectangle(422, 74, 91, 18));
			changefilename_cmd.setText("Browse...");
			changefilename_cmd.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					JFileChooser chooser = new JFileChooser();
					FileFilter filter=new WorldFileFilter();
				    chooser.setFileFilter(filter);
				    int returnVal = chooser.showOpenDialog(NewWorldDialog.this);
				    if(returnVal == JFileChooser.APPROVE_OPTION) {
				       filename = chooser.getSelectedFile().getAbsoluteFile()+".wld";
				       System.out.println("You chose to create this file: " +
				    			filename);
				       filenametxt.setText(filename);
				       
				    }
				}
			});
		}
		return changefilename_cmd;
	}

	/**
	 * This method initializes worldWidthtxt	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getWorldWidthtxt() {
		if (worldWidthtxt == null) {
			worldWidthtxt = new JTextField();
			worldWidthtxt.setBounds(new Rectangle(100, 105, 80, 21));
		}
		return worldWidthtxt;
	}

	/**
	 * This method initializes worldHeighttxt	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getWorldHeighttxt() {
		if (worldHeighttxt == null) {
			worldHeighttxt = new JTextField();
			worldHeighttxt.setBounds(new Rectangle(100, 135, 80, 21));
		}
		return worldHeighttxt;
	}

	/**
	 * This method initializes tileWidthtxt	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTileWidthtxt() {
		if (tileWidthtxt == null) {
			tileWidthtxt = new JTextField();
			tileWidthtxt.setBounds(new Rectangle(100, 165, 80, 21));
		}
		return tileWidthtxt;
	}

	/**
	 * This method initializes tileHeighttxt	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	private JTextField getTileHeighttxt() {
		if (tileHeighttxt == null) {
			tileHeighttxt = new JTextField();
			tileHeighttxt.setBounds(new Rectangle(100, 195, 80, 21));
		}
		return tileHeighttxt;
	}

	/**
	 * This method initializes jButtonOK	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setBounds(new Rectangle(85, 232, 132, 32));
			jButtonOK.setText("CREATE");
			jButtonOK.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					pressedOk = true;
					worldWidth = Integer.parseInt(worldWidthtxt.getText());
					worldHeight = Integer.parseInt(worldHeighttxt.getText());
					tileWidth = Integer.parseInt(tileWidthtxt.getText());
					tileHeight = Integer.parseInt(tileHeighttxt.getText());
					filename = filenametxt.getText();
					getInstance().setVisible(false);
				}
			});
		}
		return jButtonOK;
	}

	/**
	 * This method initializes jButtonCancel	
	 * 	
	 * @return javax.swing.JButton	
	 */
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setBounds(new Rectangle(237, 231, 121, 32));
			jButtonCancel.setText("CANCEL");
			jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
				public void actionPerformed(java.awt.event.ActionEvent e) {
					getInstance().setVisible(false);
				}
			});
		}
		return jButtonCancel;
	}
	
	public NewWorldDialog getInstance(){
		return this;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
